import { DXMResponse } from "./common";
import { invoke } from "@tauri-apps/api/core";
const dxc_name = "XComService";
const dxc_version = "0.0.1";
export type LoadServiceRequest = {
  dxc_name: string;
  dxc_version: string;
  only_build_ipc: boolean;
  config: string;
};
export type OwnerInfo = {
  owner_addr: string;
};
export type DXCInfos = {
  dxc_infos: Array<DXCInfo>;
};
export type NodeInfo = {
  xrpc_port: number;
  subscribe_xport_topic: boolean;
  subscribe_dxc_topic: boolean;
};
export type ServiceInfos = {
  service_infos: Array<ServiceInfo>;
};
export type ImportDXCInfo = {
  dxc_name: string;
  dxc_version: string;
  proto: string;
};
export type DXCDetailInfo = {
  meta: string;
  source_api: string;
  import_dxcs: Array<ImportDXCInfo>;
};
export type DXCInfo = {
  dxc_name: string;
  dxc_version: string;
};
export type BuildChannelRequest = {
  node_url: string;
  is_keep: boolean;
};
export type HashResponse = {
  value: string;
};
export type ServiceInfo = {
  dxc_name: string;
  dxc_version: string;
  md5: string;
  service_id: number;
  online_time: number;
  compatible_versions: Array<string>;
};
export type UnloadServiceRequest = {
  dxc_name: string;
  dxc_version: string;
};
export async function BuildChannel(param: BuildChannelRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "BuildChannel",
      params: param,
    },
  });
  return ret as DXMResponse<{}>;
}

export async function LoadService(param: LoadServiceRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "LoadService",
      params: param,
    },
  });
  return ret as DXMResponse<{}>;
}

export async function UnloadService(param: UnloadServiceRequest) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "UnloadService",
      params: param,
    },
  });
  return ret as DXMResponse<{}>;
}

export async function GetServiceList() {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "GetServiceList",
      params: {},
    },
  });
  return ret as DXMResponse<ServiceInfos>;
}

export async function GetAllInstalledDxc() {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "GetAllInstalledDxc",
      params: {},
    },
  });
  return ret as DXMResponse<ServiceInfos>;
}

export async function GetInstalledDxcDetail(param: DXCInfo) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "GetInstalledDxcDetail",
      params: param,
    },
  });
  return ret as DXMResponse<DXCDetailInfo>;
}

export async function SetPortAndSubscribe(param: NodeInfo) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "SetPortAndSubscribe",
      params: param,
    },
  });
  return ret as DXMResponse<{}>;
}

export async function SyncXrpcToRemote(param: ServiceInfos) {
  const ret = await invoke("invoke_dxc", {
    invokeRequest: {
      dxc_name,
      dxc_version,
      api: "SyncXrpcToRemote",
      params: param,
    },
  });
  return ret as DXMResponse<ServiceInfos>;
}
